package download;

import jdk.nashorn.internal.ir.IfNode;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ScreenShot {
    public static void getVideoPic(File video, String picPath) {
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);
        List<Frame> frameList = new ArrayList<>();
        int count = 0;
        try {
            ff.start();
            // 截取中间帧图片(具体依实际情况而定)
            int length = ff.getLengthInFrames();
            Frame frame;
            for (int j = 1; j <= length; j++) {
                frame = ff.grabFrame();
                if (frame == null) {
                    for (int i = 0; i < frameList.size(); i++) {
                       if (i >= frameList.size() - 3) {
                            getPic(picPath, frameList.get(i), i);
                        }
                    }
                    break;
                } else if (frame.image == null) {
                    length++;
                } else {
                    if (count < 3) {
                        // 截取的帧图片
                        getPic(picPath, frame, j);
                        count++;
                        if (isSimpleColorImg(picPath +File.separator+ j + ".jpg", 0.5f)) {
                            new File(picPath ,j+".jpg").delete();
                            count--;
                        }
                    }
                    if (j >= length - 3) {
                        Frame frame1 = new Frame(frame.imageWidth, frame.imageHeight, frame.imageDepth, frame.imageChannels);
                        frameList.add(frame1);
                    }
                }
            }
            ff.stop();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void getPic(String picPath, Frame frame, int j) throws IOException {
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage srcImage = converter.getBufferedImage(frame);
        int srcImageWidth = srcImage.getWidth();
        int srcImageHeight = srcImage.getHeight();

        // 对截图进行等比例缩放(缩略图)
        int width = 480;
        int height = (int) (((double) width / srcImageWidth) * srcImageHeight);
        BufferedImage thumbnailImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        thumbnailImage.getGraphics().drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        File picFile = new File(picPath,j + ".jpg");
        ImageIO.write(thumbnailImage, "jpg", picFile);
    }

    public static boolean isSimpleColorImg(String imgPath, float percent) throws IOException {
        BufferedImage src = ImageIO.read(new File(imgPath));
        int height = src.getHeight();
        int width = src.getWidth();
        int count = 0, pixTemp = 0, pixel = 0;
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                pixel = src.getRGB(i, j);
                if (pixel == pixTemp) //如果上一个像素点和这个像素点颜色一样的话，就判定为同一种颜色
                    count++;
                else
                    count = 0;
                if ((float) count / (height * width) >= percent) //如果连续相同的像素点大于设定的百分比的话，就判定为是纯色的图片
                    return true;
                pixTemp = pixel;
            }
        }
        return false;
    }

    public static void main(String[] args) throws IOException {
        String videoPath = "\\\\172.20.54.216\\home\\视觉与前端\\F10\\view\\000589_20201209.mp4";
        File video = new File(videoPath);
        String imagePath = "\\\\172.20.54.216\\home\\视觉与前端\\F10\\image";
        getVideoPic(video, imagePath);
    }

    @Test
    public void testFile(){
        File file = new File("\\\\172.20.54.216\\home\\视觉与前端\\F10\\view");
        if (file.exists() && file.isDirectory()){
            File[] files = file.listFiles();
            if (files != null) {
                for (File file1 : files) {
                    String name = file1.getName();
                    String[] split = name.split("\\.");
                }
            }
        }
    }
}
